package com.zl.controller;

import com.zl.domain.Contract;
import com.zl.domain.Query;
import com.zl.service.ContractService;
import net.sf.json.JSONObject;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class ContractController {
    @Autowired
    private ContractService contractService;

    @RequestMapping(value = "/contract/findAllTypes", method = RequestMethod.GET)
    public
    @ResponseBody
    JSONObject findTypes() {
        return contractService.findTypes();
    }

    @RequestMapping(value = "/contract/contractList", method = RequestMethod.GET)
    public
    @ResponseBody
    JSONObject contractList(Query query) {
        return new JSONObject().accumulate("page", contractService.page(query));
    }

    @RequestMapping(value = "/contract/delContract", method = RequestMethod.GET)
    public
    @ResponseBody
    JSONObject delContract(@Param("Contract") Contract contract) {
        return new JSONObject().accumulate("message", contractService.delete(contract) == 0 ? "删除失败！！" : "删除成功！");
    }

    @RequestMapping(value = "/contract/editContract", method = RequestMethod.POST)
    public
    @ResponseBody
    JSONObject editContract(@Param("Contract") Contract contract) {
        String message = "";
        if (contract.getCid() == null || ("").equals(contract.getCid()))
            message = contractService.insert(contract) == 0 ? "添加失败！！" : "添加成功！";
        else
            message = contractService.update(contract) == 0 ? "编辑失败！！" : "编辑成功！";
        return new JSONObject().accumulate("message", message);
    }

    @RequestMapping(value = "/contract/analysis", method = RequestMethod.GET)
    public
    @ResponseBody
    JSONObject analysis(String type, String group, String data) {
        return contractService.analysis(type, group, data);
    }
}
